﻿namespace WhoCanHelpMe.Tasks
{
    #region Using Directives

    using System.Collections.Generic;
    using System.Linq;

    using Domain;
    using Domain.Contracts.Repositories;
    using Domain.Contracts.Tasks;
    using Domain.Specifications;

    #endregion

    public class SearchTasks : ISearchTasks
    {
        private readonly IAssertionRepository assertionRepository;

        public SearchTasks(IAssertionRepository assertionRepository)
        {
            this.assertionRepository = assertionRepository;
        }

        public IList<Assertion> ByTag(string tagName)
        {
            return this.DoSearch(new AssertionByTagNameSpecification(tagName));
        }

        public IList<Assertion> ByTagId(int tagId)
        {
            return this.DoSearch(new AssertionByTagIdSpecification(tagId));
        }

        private IList<Assertion> DoSearch(QuerySpecification<Assertion> specification)
        {
            return this.assertionRepository
                .FindAll(specification)
                .OrderBy(o => o.Profile.LastName)
                .ToList();
        }
    }
}
